//
//  spacex.cpp
//  Space Explorer
//
//  Created by Yann Blaudin de Thé on 05/04/13.
//  Copyright (c) 2013 TeaTime. All rights reserved.
//

#include "SEOrbitalElements.h"
#include "SEWorld.h"
#include "SEEventReceiver.h"

#define ua 149597870.610
#define dy 86400.


int main(int argc, char **argv) {
    SEEventReceiver* event_receiver = new SEEventReceiver();
    
    SEWorld world = SEWorld(event_receiver);
    event_receiver->setWorld(&world);
    
    world.loadBSC(GetApplicationResourcesPath() + "BSC_catalog.txt");
    
    std::string g_file, n_file, c_file;
    
    g_file= GetApplicationResourcesPath() + "sun.jpg";
    SEPlanet* sun = world.createPlanet(NULL, "Soleil", "Sun", g_file, "", "");
    
    g_file= GetApplicationResourcesPath() + "mercury_ground.jpg";
    SEPlanet* mecr = world.createPlanet(NULL, "Mercure", "Mercury", g_file, "", "");
    
    g_file= GetApplicationResourcesPath() + "venus_ground.jpg";
    SEPlanet* venu = world.createPlanet(NULL, "Venus", "Venus", g_file, "", "");
    
    n_file = GetApplicationResourcesPath() + "earth_normal.jpg";
    g_file = GetApplicationResourcesPath() + "earth_ground.jpg";
    c_file = GetApplicationResourcesPath() + "earth_cloud.jpg";
    SEPlanet* earth = world.createPlanet(sun, "Terre", "Earth", g_file, "", "");

    n_file = GetApplicationResourcesPath() + "moon_normal.jpg";
    g_file = GetApplicationResourcesPath() + "moon_ground.jpg";
    SEPlanet* moon = world.createPlanet(earth, "Lune", "Moon", g_file, "", n_file);
    
    n_file = GetApplicationResourcesPath() + "mars_normal.jpg";
    g_file= GetApplicationResourcesPath() + "mars_ground.jpg";
    SEPlanet* mars = world.createPlanet(NULL, "Mars", "Mars", g_file, "", n_file);
    
    irr::scene::ILightSceneNode* light = world.createLight(sun);
    
//    SEVessel *shuttle = world.createVessel("Shuttle", "/Users/ydethe/Documents/XCode-Projects/App Mac OS X/Space Explorer MP/Shuttle/SpaceShuttleOrbiter.3ds");
    
//    mat4f mat;
//    mat.setTranslation(vec3f(1,-2,3));
//    // Roll, Pitch, Yaw
//    mat.setRotationDegrees(vec3f(10,-100,30));
//    
//    disp_vec3(mat.getRotationDegrees());
//    std::cout << std::endl;    
    
    while(world.run()) {
        world.loop();

    }
    
    delete event_receiver;
    
    std::cout << "Fin normale" << std::endl;
    
	return 0;
    
}


/*
#include <irrlicht.h>
#include "btBulletDynamicsCommon.h"
#include <iostream>

using namespace irr;


using namespace core;
using namespace scene;
using namespace video;
using namespace io;
using namespace gui;

void QuaternionToEuler(const btQuaternion &TQuat, btVector3 &TEuler)
{
    btScalar W = TQuat.getW();
    btScalar X = TQuat.getX();
    btScalar Y = TQuat.getY();
    btScalar Z = TQuat.getZ();
    float WSquared = W * W;
    float XSquared = X * X;
    float YSquared = Y * Y;
    float ZSquared = Z * Z;
    
    TEuler.setX(atan2f(2.0f * (Y * Z + X * W), -XSquared - YSquared + ZSquared + WSquared));
    TEuler.setY(asinf(-2.0f * (X * Z - Y * W)));
    TEuler.setZ(atan2f(2.0f * (X * Y + Z * W), XSquared - YSquared - ZSquared + WSquared));
    TEuler *= core::RADTODEG;
}

int main(int argc, char** argv)
{
    //Creating the irrlicht device
    IrrlichtDevice *device =createDevice(EDT_OPENGL, dimension2d<u32>(800, 600), 16, false, false, false, 0);
    
    //setting pointers to the device!
    IVideoDriver* driver = device->getVideoDriver();
    ISceneManager* smgr = device->getSceneManager();
    IGUIEnvironment* guienv = device->getGUIEnvironment();
    
    //creating the 3d ball
    ISceneNode *irrball = smgr->addSphereSceneNode();
    irrball->setMaterialTexture( 0, driver->getTexture("/Users/ydethe/Documents/XCode-Projects/Librairies/irrlicht-1.8/media/wall.jpg") );
    irrball->setMaterialFlag(EMF_LIGHTING,false);
    
    //creating the camera
    ICameraSceneNode *irrcam = smgr->addCameraSceneNode();
    irrcam->setPosition(vector3df(0,0,-70));
    
    //creating the broadphase
    btBroadphaseInterface* broadphase = new btDbvtBroadphase();
    
    //creating the physics properties configuration
    btDefaultCollisionConfiguration* collisionConfiguration = new btDefaultCollisionConfiguration();
    btCollisionDispatcher* dispatcher = new btCollisionDispatcher(collisionConfiguration);
    
    //creating the solvers
    btSequentialImpulseConstraintSolver* solver = new btSequentialImpulseConstraintSolver;
    
    //Making the dynamic world
    btDiscreteDynamicsWorld* dynamicsWorld = new btDiscreteDynamicsWorld(dispatcher,broadphase,solver,collisionConfiguration);
    
    //setting the gravity
    dynamicsWorld->setGravity(btVector3(0,-9.81,0));
    
    
    //Its time to create the shapes for our collision
    //Creating the ground( a simple plane )
    btBoxShape* groundShape = new btBoxShape(btVector3(btScalar(50.),btScalar(50.),btScalar(50.)));
    
    //Creating the ball shape
    btCollisionShape* fallShape = new btSphereShape(1);
    
    //its time to create the ground object
    btDefaultMotionState* groundMotionState = new btDefaultMotionState(btTransform(btQuaternion(0,0,0,1),btVector3(0,-1,0)));
    
    //making the ground rigidody
    btRigidBody::btRigidBodyConstructionInfo groundRigidBodyCI(0,groundMotionState,groundShape,btVector3(0,0,0));
    btRigidBody* groundRigidBody = new btRigidBody(groundRigidBodyCI);
    
    //adding the ground to the physics world
    dynamicsWorld->addRigidBody(groundRigidBody);
    
    //its time to create the ball object!!!
    btDefaultMotionState* fallMotionState = new btDefaultMotionState(btTransform(btQuaternion(0,0,0,1),btVector3(0,50,0)));
    
    //adding and calculating the ball sphere mas
    btScalar mass = 1;
    btVector3 fallInertia(0,0,0);
    fallShape->calculateLocalInertia(mass,fallInertia);
    
    //constructing the rigidody(adding info,etc)
    btRigidBody::btRigidBodyConstructionInfo fallRigidBodyCI(mass,fallMotionState,fallShape,fallInertia);
    
    //Creating the ball rigidbody
    btRigidBody* fallRigidBody = new btRigidBody(fallRigidBodyCI);
    
    //And offcourse add it to the world
    dynamicsWorld->addRigidBody(fallRigidBody);
    
    btTransform trans;
    btVector3 rot;
    
    while (device->run())
    {
        //getting the world positions(x,y,z)
        fallRigidBody->getMotionState()->getWorldTransform(trans);
        
        //converting the bullet rotation axes so we can use it with our rlicht node
        QuaternionToEuler(trans.getRotation(),rot);
        
        //adding the position and rotation to the node
        irrball->setPosition(vector3df(trans.getOrigin().getX(),trans.getOrigin().getY(),trans.getOrigin().getZ()));
        irrball->setRotation(vector3df(rot.getX(),rot.getY(),rot.getZ()));
        //stepping the simulation
        dynamicsWorld->stepSimulation(1/60.f,10);
        
        std::cout << "sphere height: " << trans.getOrigin().getY() << std::endl;
        
        driver->beginScene(true, true, SColor(0,200,200,200));
        
        smgr->drawAll();
        guienv->drawAll();
        
        driver->endScene();
    }
    
    // Clean up behind ourselves like good little programmers
    dynamicsWorld->removeRigidBody(fallRigidBody);
    delete fallRigidBody->getMotionState();
    delete fallRigidBody;
    
    dynamicsWorld->removeRigidBody(groundRigidBody);
    delete groundRigidBody->getMotionState();
    delete groundRigidBody;
    
    
    delete fallShape;
    
    delete groundShape;
    
    
    delete dynamicsWorld;
    delete solver;
    delete collisionConfiguration;
    delete dispatcher;
    delete broadphase;
    
    device->drop();
    
    return 0;
}*/


